package club.dataresources.service.impl;

import club.dataresources.entity.*;
import club.dataresources.mapper.BarberMapper;
import club.dataresources.mapper.OrderMapper;
import club.dataresources.mapper.WaiterMapper;
import club.dataresources.service.BarberService;
import club.dataresources.service.MenuService;
import club.dataresources.service.OrderService;
import club.dataresources.service.WaiterService;
import club.dataresources.util.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author
 * @create 2018-11-12 1:16
 **/
@Service
public class OrderServiceImpl implements OrderService{
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private MenuService menuService;

    @Autowired
    private WaiterService waiterService;

    @Autowired
    private BarberService barberService;

    @Autowired
    private BarberMapper barberMapper;

    @Autowired
    private WaiterMapper waiterMapper;

    @Override
    public boolean addOrder(Order order) throws Exception {
        return orderMapper.insertSelective(order)==1;
    }

    @Override
    public boolean removeOrder(Integer orderId) throws Exception {
        return orderMapper.deleteByPrimaryKey(orderId)==1;
    }

    @Override
    public boolean updateOrder(Order order) throws Exception {
        return orderMapper.updateByPrimaryKeySelective(order)==1;
    }

    @Override
    public Order findByOrderId(Integer orderId) throws Exception {
        return orderMapper.selectByPrimaryKey(orderId);
    }

    @Override
    public List<Order> findOrderByCustomerId(Integer customerId) throws Exception {
        return orderMapper.findOrderByCustomerId(customerId);
    }

    @Override
    public Page<Order> listOrder(Integer page, Integer rows, Order order) throws Exception {
        PageHelper.startPage(page, rows);
        OrderExample example = new OrderExample();
        OrderExample.Criteria criteria = example.createCriteria();
        if(order.getOrderStatus()!=null){
            criteria.andOrderStatusLike(order.getOrderStatus());
        }
        if(order.getCustomerId()!=null){
            criteria.andCustomerIdEqualTo(order.getCustomerId());
        }
        criteria.andOrderIdIsNotNull();
        return orderMapper.selectByExample(example);
    }

    /**
     * 用户根据套餐下订单
     * 系统默认分配一个时间上最邻近的理发师(同样邻近时选择价格低的)
     * 筛选出理发师之后如果该理发师的该日期当天没空，顺延该理发师的下一个工作日找可预约时间段
     *
     * 进入订单确定页面，如不满意可以点击修改，跳转到理发师预约页面
     *
     * @param menuId
     * @param customerId
     * @return
     * @throws Exception
     */
    @Override
    public Order getMenuOrder(Integer menuId, Integer customerId) throws Exception {
        Order order = new Order();

        //设置套餐id
        order.setMenuId(menuId);

        //设置下订单时间
        order.setStartTime(DateUtil.currentDatetime());

        //设置用户id
        Integer id = customerId;
        order.setCustomerId(id);

        //设置订单号(时间戳+customerId+menuId)
        Long orderNum = System.currentTimeMillis() + Long.valueOf(id) + Long.valueOf(menuId);
        order.setOrderNum(String.valueOf(orderNum));

        //设置订单状态
        order.setOrderStatus("等待支付");

        //获得一个理发师barber_id
        Barber barber = barberService.getBarber();
        if(barber==null){
            throw new IllegalArgumentException("随机获取一个理发师barber_id失败");
        }else{
            order.setBarberId(order.getBarberId());
        }

        //获得一个服务员waiter_id
        Waiter waiter = waiterService.getRandomWaiter();
        order.setWaiterId(waiter.getWaiterId());

        //设置订单详情
        Menu menu = menuService.getMenuById(menuId);
        String orderDetail_menu = "您将预定套餐:"+menu.getMenuName()+" ("+menu.getMenuDescription()+") ";
        String orderDetail_barber = "并由理发师:"+barber.getBarberName()+" ("+barber.getBarberLevel()+") ";
        String orderDetail_waiter = "服务员:"+waiter.getWaiterName()+"为您服务";
        String orderDetail = orderDetail_menu.concat(orderDetail_barber).concat(orderDetail_waiter);
        order.setOrderDetail(orderDetail);

        //计算订单总价格(加上理发师服务费)
        Float orderPrice = menu.getMenuPrice() + barber.getServiceCharge();
        order.setOrderPrice(orderPrice);


        //设置订单预约状态
        order.setBarberorderedIs(0);
        order.setBarberorderedTime(null);
        return order;
    }

    /**
     * 展示该用户最新的一个订单详情
     * @param order
     * @return
     * @throws Exception
     */
    @Override
    public Order checkMenuOrder(Order order) throws Exception {
        OrderExample example = new OrderExample();
        OrderExample.Criteria criteria = example.createCriteria();

        criteria.andCustomerIdEqualTo(order.getCustomerId());
        criteria.andOrderNumEqualTo(order.getOrderNum());

        List<Order> liOrder = orderMapper.selectByExample(example);
        if(liOrder!=null && liOrder.size()>0){
            return liOrder.get(0);
        }else{
            return null;
        }
    }

    /**
     * 在订单确认的时候更改理发师
     * @param order
     * @param barberId
     * @return
     * @throws Exception
     */
    @Override
    public Order changeBarberForOrder(Order order,Integer barberId) throws Exception {
        order.setBarberId(barberId);

        Menu menu = menuService.getMenuById(order.getMenuId());
        Barber barber = barberMapper.selectByPrimaryKey(barberId);
        if(barber==null){
            throw new IllegalArgumentException("您更改的理发师为空");
        }
        Waiter waiter = waiterMapper.selectByPrimaryKey(order.getWaiterId());
        String orderDetail_menu = "您将预定套餐:"+menu.getMenuName()+" ("+menu.getMenuDescription()+") ";
        String orderDetail_barber = "并由理发师:"+barber.getBarberName()+" ("+barber.getBarberLevel()+") ";
        String orderDetail_waiter = "服务员:"+waiter.getWaiterName()+"为您服务";
        String orderDetail = orderDetail_menu.concat(orderDetail_barber).concat(orderDetail_waiter);
        order.setOrderDetail(orderDetail);

        Float orderPrice = menu.getMenuPrice() + barber.getServiceCharge();
        order.setOrderPrice(orderPrice);
        return order;
    }
}
